﻿using System;
using System.Windows.Forms;

namespace DropboxImageStorage
{
	public partial class OAuthControl : UserControl
	{
		public event EventHandler SignIn;
		private void InvokeSignIn()
		{
			EventHandler handler = SignIn;
			if (handler != null) handler(this, EventArgs.Empty);
		}

		public event EventHandler CancelWaiting;
		private void InvokeCancelWaiting()
		{
			EventHandler handler = CancelWaiting;
			if (handler != null) handler(this, EventArgs.Empty);
		}

		public event EventHandler Unlink;
		private void InvokeUnlink()
		{
			EventHandler handler = Unlink;
			if (handler != null) handler(this, EventArgs.Empty);
		}

		public AuthState AuthState { get; private set; }

		public OAuthControl()
		{
			InitializeComponent();

			pnlSignIn.Top = 0;
			pnlSignIn.Left = 0;
			pnlSignIn.Dock = DockStyle.Fill;

			pnlWaitAuth.Top = 0;
			pnlWaitAuth.Left = 0;
			pnlWaitAuth.Dock = DockStyle.Fill;

			pnlUnlink.Top = 0;
			pnlUnlink.Left = 0;
			pnlUnlink.Dock = DockStyle.Fill;

			SetState(AuthState.NotAuthorized);
		}

		public void SetState(AuthState state)
		{
			pnlSignIn.Visible = state == AuthState.NotAuthorized;
			pnlWaitAuth.Visible = state == AuthState.WaitAuthorization;
			pnlUnlink.Visible = state == AuthState.Authorized;

			AuthState = state;
		}

		private void btnSignIn_Click(object sender, EventArgs e)
		{
			InvokeSignIn();
		}

		private void btnCancelAuthWaiting_Click(object sender, EventArgs e)
		{
			InvokeCancelWaiting();
		}

		private void btnUnlink_Click(object sender, EventArgs e)
		{
			InvokeUnlink();
		}
	}

	public enum AuthState
	{
		NotAuthorized,
		WaitAuthorization,
		Authorized
	}
}
